package com.marvels.product.param.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dao.McptAmortMethodMapper;
import com.marvels.product.param.dto.McptAmortMethodDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.AmortMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AmortMethodServiceImpl implements AmortMethodService {

	@Autowired
	private McptAmortMethodMapper mcptAmortMethodMapper;
	
	@Override
	public PageInfo<McptAmortMethodDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptAmortMethodDTO>  mcptConsultingFeeList = mcptAmortMethodMapper.getListByParam(param);
		
		PageInfo<McptAmortMethodDTO> pageInfo = new PageInfo<McptAmortMethodDTO>(mcptConsultingFeeList);
	    
		return pageInfo;
	}

	@Override
	@Transactional
	public int insert(McptAmortMethodDTO dto) throws MarvelsBusinessException{
		return mcptAmortMethodMapper.insert(dto);
	}

	@Override
	public McptAmortMethodDTO getEntityByPrimaryKey(Long amortId) throws MarvelsBusinessException{
		return mcptAmortMethodMapper.selectByPrimaryKey(amortId);
	}

	@Transactional(rollbackFor=Exception.class)
	public int updateByPrimaryKey(McptAmortMethodDTO dto) throws MarvelsBusinessException{
		Integer count = 0;
		try {
			 String userName = MarvelsConstant.SYSTEM_USER;
			 UserDTO user = CacheUtils.getCacheUser();
			 if(user!=null){
				 userName = user.getUserName();
			 }
			 dto.setUpdatedBy(userName);
			 count  = mcptAmortMethodMapper.updateByPrimaryKey(dto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>更新还款计算方法异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}

	@Override
	public void updateStatus(McptAmortMethodDTO dto) throws MarvelsBusinessException{
		updateByPrimaryKey(dto);
	}

	@Override
	public McptAmortMethodDTO getAmortMethodByAmortMethodCode(McptProduct dto) throws MarvelsBusinessException {
		return mcptAmortMethodMapper.selectAmortMethodByAmortMethodCode(dto);
	}

	
	public int delete(McptAmortMethodDTO dto) throws MarvelsBusinessException {
		dto.setDeleteFlag(1);
		return updateByPrimaryKey(dto);
	}

}
